<template>
    <div class="main">
      <el-card class="search-box">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
          <el-form-item label="项目名称" prop="massageName">
            <el-input
              v-model="queryParams.massageName"
              placeholder="请输入项目名称"
              clearable
              @keyup.enter.native="getList"
            />
          </el-form-item>
          <el-form-item label="状态" prop="state">
            <el-select v-model="queryParams.isSale" placeholder="请选择" clearable>
              <el-option label="上架" :value="true" />
              <el-option label="下架" :value="false" />
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间" prop="time">
            <el-date-picker
            v-model="value"
              @change="selectTime"
              format="yyyy-MM-dd HH:mm:ss" 
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="getList"
              >查询</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </el-card>
      <el-card class="table-box">
        <el-button
          type="primary"
          class="el-icon-plus"
          size="mini"
          @click="addGoods()"
          >添加项目</el-button
        >
        <el-table
          :data="tableData"
          v-loading="loading"
          :header-cell-style="{
            background: '#f5f7fa',
            color: '#000000',
          }"
          style="width: 100%"
        >
          <el-table-column prop="msg" label="项目描述"  width="280"> 
            <template slot-scope="scope">
              <div class="msg">
                <img :src="scope.row.massageThumb" alt="">
                <p>{{scope.row.massageName}}</p>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="salePrice" label="商品售价"> </el-table-column>
          <el-table-column prop="originalPrice" label="划线价"> </el-table-column>
          <el-table-column prop="state" label="状态">
            <template slot-scope="scope">
              <el-switch
                class="switchStyle"
                @change="updateDown(scope.row.isSale,scope.row.id)"
                v-model="scope.row.isSale"
                active-text="上架"
                inactive-text="下架"
              >
            </el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间"> </el-table-column>
          <el-table-column prop="edit" label="操作"> 
            <template slot-scope="scope">
              <el-button  size="mini" type="primary" @click="handleUpdate(scope.row.id)">
                编辑
                <!-- {{scope.row.state}} -->
              </el-button>
              <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNumber" :limit.sync="queryParams.pageSize" @pagination="getList" />
      </el-card>
      <el-dialog
      title="提示"
      :visible.sync="dialogVisibleClose"
      width="30%"
      :before-close="handleDeleteClose">
      <span>确定进行删除吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleClose = false">取 消</el-button>
        <el-button type="primary" @click="submitDelete()">确 定</el-button>
      </span>
    </el-dialog>
    </div>
  </template>
  
  <script>
  import Pagination from '@/components/Pagination'
  import {queryItemList,deleteProject,updateSaleFlag} from '@/api/project'
  export default {
    name: "",
    components: { Pagination },
    props: {},
    data() {
      return {
        loading: true,
        queryParams: {
          pageNumber:1,
          pageSize:10,
          startTime:'',
          endTime:'',
          massageName:'',
          isSale: null
        },
        status:1,
        total: null,
        tableData: [],
        dialogVisibleClose:false,
        projectId:null,
        value:''
      };
    },
    watch: {},
    computed: {},
    methods: {
      addGoods(){
        this.$router.push('/project/add')
      },
      selectTime(val){
        this.queryParams.startTime = val[0]+''
        this.queryParams.endTime = val[1]+''
      },
      async getList(){
        const res = await queryItemList(this.queryParams);
        this.loading =false
        this.tableData = res.data.list
        this.total = res.data.total
      },
      resetQuery() {
        this.queryParams={
          pageNumber:1,
          pageSize:10,
          startTime:'',
          endTime:'',
          massageName:'',
          isSale: null
        }
        this.getList()
      },
      handleDeleteClose(){
        this.dialogVisibleClose = false
      },
      handleDelete(id){
        this.dialogVisibleClose = true
        this.projectId = id
      },
      handleUpdate(id){
        this.$router.push({path:'/project/edit',query: {id}})
      },
      submitDelete(){
        deleteProject(this.projectId).then(res=>{
          if(res.code == 200){
            this.$message({
              message: res.message,
              type: 'success'
            });
            this.getList()
            this.dialogVisibleClose = false
          }

        })
      },
      updateDown(type,id){
        let params = {
          scene:type?'UP':'DOWN'
        }
        updateSaleFlag(params,id).then(res=>{
          this.$message({
              message: res.message,
              type: 'success'
          });
          this.getList()
        })
      }
    },
    created() {
      this.getList()
    },
    mounted() {},
  };
  </script>
  
  <style lang="scss" scoped>
    .el-form-item{
      margin-right: 20px;
    }
    .table-box{
      min-height: calc(100vh - 220px);
      margin-top: 10px;
    }
    .el-table{
      margin-top: 20px;
    }
    ::v-deep .switchStyle .el-switch__label {
      position: absolute;
      display: none;
      color: #fff;
    }
    ::v-deep .switchStyle .el-switch__label--left {
      z-index: 9;
      left: 19px;
    }
    ::v-deep .switchStyle .el-switch__label--right {
      z-index: 9;
      left: -2px;
    }
    ::v-deep .el-switch__label.is-active {
      display: block;
    }
    ::v-deep .el-switch .el-switch__core,
    .el-switch .el-switch__label {
      width: 60px !important;
    } 
    .msg{
      display: flex;
      align-items: center;
      img{
        margin-right: 10px;
        width: 80px;
      }
    }
  </style>